package com.laowang.project.system.controller;

import java.util.Date;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.laowang.common.utils.SecurityUtils;
import com.laowang.framework.web.page.TableDataInfoMy;
import com.laowang.project.system.domain.MyCyhShop;
import com.laowang.project.system.service.MyCyhShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.laowang.framework.aspectj.lang.annotation.Log;
import com.laowang.framework.aspectj.lang.enums.BusinessType;

import org.springframework.web.bind.annotation.*;
import com.laowang.project.system.domain.MyManagerCoupon;
import com.laowang.project.system.service.MyManagerCouponService;
import com.laowang.framework.web.controller.BaseController;
import com.laowang.framework.web.domain.AjaxResult;
import com.laowang.common.utils.poi.ExcelUtil;
import com.laowang.framework.web.page.TableDataInfo;

import javax.validation.constraints.NotNull;

/**
 * 定制的商家优惠券Controller
 *
 * @author laowang
 * @date 2023-08-17
 */
@Api(tags = "我的系统券")
@RestController
@RequestMapping("/managerCoupon")
public class MyManagerCouponController extends BaseController {

    @Autowired
    private MyManagerCouponService myManagerCouponService;

    @Autowired
    private MyCyhShopService myCyhShopService;

    /**
     * 我的系统券列表
     */
    @ApiOperation(value = "小程序端 查询 个人定制的商家优惠券列表",notes = "")
    @GetMapping("/web/list")
    public TableDataInfoMy weblist(MyManagerCoupon myManagerCoupon) {
        @NotNull(message = "页数不能为空") Integer pageNum = myManagerCoupon.getPageNum();
        @NotNull(message = "条数不能为空") Integer pageSize = myManagerCoupon.getPageSize();
        PageHelper.startPage(pageNum,pageSize);
        Integer mid = SecurityUtils.getLoginUser().getWxUser().getMid();
        myManagerCoupon.setUserId(mid);
        List<MyManagerCoupon> list = myManagerCouponService.selectMyManagerCouponListWeb(myManagerCoupon);
        return getDataTableMy(list);
    }

    @ApiOperation(value = "小程序端 券核销",notes = "")
    @GetMapping("/check")
    public AjaxResult checklist(Integer id,Integer shop) {
        MyManagerCoupon myManagerCoupon = myManagerCouponService.selectMyManagerCouponById(id);
        if (myManagerCoupon == null){
            return AjaxResult.error("此券为空");
        }
        //商户 的用户id
        Integer mid = SecurityUtils.getLoginUser().getWxUser().getMid();

        Date expireTime = myManagerCoupon.getExpireTime();
        if (expireTime != null && expireTime.after(new Date())){
            return AjaxResult.error("此券已过期");
        }

        Integer shopId = myManagerCoupon.getShopId();
        if (shopId !=null){
            MyCyhShop myCyhShop = new MyCyhShop();
            myCyhShop.setUserId(mid);
            List<MyCyhShop> myCyhShops = myCyhShopService.selectMyCyhShopListWeb(myCyhShop);
            Boolean flag =false;
            for (MyCyhShop cyhShop : myCyhShops) {
                if (cyhShop.getId().equals(shopId)){
                    flag = true;
                }
            }
            if (!flag){
                return AjaxResult.error("不能核销其他商家的券");
            }

        }else {




        }

        Integer num = myManagerCoupon.getNum();
        if (num>1){
            num = num -1;
            myManagerCoupon.setNum(num);

            MyManagerCoupon myManagerCoupon1 = new MyManagerCoupon();
            myManagerCoupon1  =  myManagerCoupon;
            if (shopId !=null){
                myManagerCoupon1.setCheckShop(shopId);
            }else {
                if (shop == null){
                    return AjaxResult.error("请选择核销商家");
                }
                MyCyhShop myCyhShop = myCyhShopService.selectMyCyhShopById(shop);
                if (myCyhShop == null){
                    return AjaxResult.error("请选择核销的商家不存在");
                }
                myManagerCoupon1.setCheckShop(shop);
            }
            myManagerCouponService.updateMyManagerCoupon(myManagerCoupon);
            myManagerCoupon1.setId(null);
            myManagerCoupon1.setIsCheck(1);
            myManagerCoupon1.setCheckTime(new Date());
            myManagerCouponService.insertMyManagerCoupon(myManagerCoupon1);


        }else {
            if (shopId == null){
                if (shop == null){
                    return AjaxResult.error("请选择核销商家");
                }
                MyCyhShop myCyhShop = myCyhShopService.selectMyCyhShopById(shop);
                if (myCyhShop == null){
                    return AjaxResult.error("请选择核销的商家不存在");
                }
                myManagerCoupon.setCheckShop(shop);
            }
            myManagerCoupon.setIsCheck(1);
            myManagerCoupon.setCheckTime(new Date());
            myManagerCouponService.updateMyManagerCoupon(myManagerCoupon);
        }

        return AjaxResult.success();
    }

    /**
     * 查询定制的商家优惠券列表
     */
    @ApiOperation(value = "查询定制的商家优惠券列表",notes = "")
    @PreAuthorize("@ss.hasPermi('my:managerCoupon:list')")
    @GetMapping("/list")
    public TableDataInfo list(MyManagerCoupon myManagerCoupon) {
        startPage();
        List<MyManagerCoupon> list = myManagerCouponService.selectMyManagerCouponList(myManagerCoupon);
        return getDataTable(list);
    }

    /**
     * 导出定制的商家优惠券列表
     */
    @ApiOperation(value = "导出定制的商家优惠券列表",notes = "")
    @PreAuthorize("@ss.hasPermi('my:managerCoupon:export')")
    //@Log(title = "定制的商家优惠券", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MyManagerCoupon myManagerCoupon) {
        List<MyManagerCoupon> list = myManagerCouponService.selectMyManagerCouponList(myManagerCoupon);
        ExcelUtil<MyManagerCoupon> util = new ExcelUtil<MyManagerCoupon>(MyManagerCoupon.class);
        return util.exportExcel(list, "managerCoupon");
    }

    /**
     * 获取定制的商家优惠券详细信息
     */
    @ApiOperation(value = "获取定制的商家优惠券详细信息",notes = "")
    @PreAuthorize("@ss.hasPermi('my:managerCoupon:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(myManagerCouponService.selectMyManagerCouponById(id));
    }

    /**
     * 新增定制的商家优惠券
     */
    @ApiOperation(value = "新增定制的商家优惠券",notes = "")
    @PreAuthorize("@ss.hasPermi('my:managerCoupon:add')")
    //@Log(title = "定制的商家优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MyManagerCoupon myManagerCoupon) {

        return toAjax(myManagerCouponService.insertMyManagerCoupon(myManagerCoupon));
    }

    /**
     * 修改定制的商家优惠券
     */
    @ApiOperation(value = "修改定制的商家优惠券",notes = "")
    @PreAuthorize("@ss.hasPermi('my:managerCoupon:edit')")
    //@Log(title = "定制的商家优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MyManagerCoupon myManagerCoupon) {
        return toAjax(myManagerCouponService.updateMyManagerCoupon(myManagerCoupon));
    }

    /**
     * 删除定制的商家优惠券
     */
    @ApiOperation(value = "删除定制的商家优惠券",notes = "")
    @PreAuthorize("@ss.hasPermi('my:managerCoupon:remove')")
    //@Log(title = "定制的商家优惠券", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(myManagerCouponService.deleteMyManagerCouponByIds(ids));
    }
}
